<?php

namespace app\api\Service;

use think\Config;

class IndexService
{
    // 获取支付方式
    public static function getPayment()
    {
        $list = Config::get('site.payment');
        $data = ['alipay'=>false,'wechat'=>false,'wechat_login'=>false];
        $wechat_login = Config::get('site.wechat_login');
        foreach($list as $row){
            if($row == 'alipay') $data['alipay'] = true;
            if($row == 'wechat') $data['wechat'] = true;
        }
        if($wechat_login) $data['wechat_login'] = true;

        return ['code'=>1,'msg'=>'查询成功','data'=>$data];
    }

    // 获取引导页
    public static function getGuidePage()
    {
        $list = \app\admin\model\Guide::where(['status'=>'normal'])->order('weigh desc')->field('image')->select();
        $total = count($list);
        return ['code'=>1,'msg'=>'查询成功','data'=>['total'=>$total,'list'=>$list]];
    }

    // 获取轮播图
    public static function getBanner()
    {
        $list = \app\admin\model\Banner::where(['status'=>'normal'])->order('weigh desc')->field('type,image,url')->select();
        $total = count($list);
        return ['code'=>1,'msg'=>'查询成功','data'=>['total'=>$total,'list'=>$list]];
    }

    // 获取客服二维码
    public static function getCustomer()
    {
        $data = Config::get('site.customer');
        return ['code'=>1,'msg'=>'查询成功','data'=>$data];
    }

    // 获取协议
    public static function getProtocol($data)
    {
        $id = $data['id'];
        $details = \app\admin\model\Protocol::where(['id'=>$id,'status'=>'normal'])->field('title,content')->find();
        return ['code'=>1,'msg'=>'查询成功','data'=>$details];
    }

    // 获取常见问题
    public static function getQuestion($data)
    {
        $limit = isset($data['limit'])? $data['limit'] : 10;
        $list = \app\admin\model\Question::where(['status'=>'normal'])->field('title,content')->paginate($limit);
        return ['code'=>1,'msg'=>'查询成功','data'=>$list];
    }

    // 回调测试
    public static function notifyxTest()
    {
        $data = '{"gmt_create":"2024-07-10 14:13:52","charset":"utf-8","seller_email":"1062818043@qq.com","subject":"【加钟套餐2】","sign":"ftTnqkFjXWuYD597bd0uSl6mLikXmilOGT3dYJA6s2uwaa5BaHukrW90mUt63lQRhTgC6U\/eVXjBPpz583qZbLFmrvEBssvI1Qmp4Ilh7FtbryKxFv7dxllpWsEisaW529QA4QB1CUIzpmE\/fEmsIqxTnykV\/l6Sqd6SVGvDB3NtXdxYSdJhzs2OCe5jnInNt3diHKr\/fUkXE2CvQV4AUiWUdFfWcSjXOgK4nNvouiAFujVpUWsblJYVDnVPxLPAubxcag1kBkXH\/K2aTUcinEACcexFFORM0y1ZC2uju45qNzCjc+F9jjiGydydHwPrHi+4SAf7Y5kh3BKkuhKObg==","buyer_open_id":"095zHdmpXKTr4mHIwnEqrKrwI6TAGuFYymbLSanFNAnXIgb","invoice_amount":"0.01","notify_id":"2024071001222141353056951477783506","fund_bill_list":"[{\"amount\":\"0.01\",\"fundChannel\":\"ALIPAYACCOUNT\"}]","notify_type":"trade_status_sync","trade_status":"TRADE_SUCCESS","receipt_amount":"0.01","app_id":"2021004154686341","buyer_pay_amount":"0.01","sign_type":"RSA2","seller_id":"2088841585147720","gmt_payment":"2024-07-10 14:13:52","notify_time":"2024-07-10 14:13:53","merchant_app_id":"2021004154686341","version":"1.0","out_trade_no":"E20240710857557884659","total_amount":"0.01","trade_no":"2024071022001456951441537720","auth_app_id":"2021004154686341","buyer_logon_id":"188****3906","point_amount":"0.00"}';
        $data = json_decode($data,true);
        $paytype = 'alipay';

        if($paytype == 'alipay'){
            $first = substr($data['out_trade_no'], 0, 1);
            if($first == 'S'){
                $order = \app\admin\model\Order::where('order_no', $data['out_trade_no'])->find();
                if(!$order){
                    return json(['code' => 'FAIL', 'message' => '订单不存在'], 500, ['Content-Type' => 'application/json']);
                }
                //判断是否已支付过
                if( $order->paid == 1 ){
                    return json(['code' => 'FAIL', 'message' => '订单已支付'], 500, ['Content-Type' => 'application/json']);
                }
                //修改订单状态
                $order->trade_no = $data['trade_no'];
                $order->paid = 1;
                $order->status = 0;
                $order->paidtime = time();
                $order_result = $order->save();

            }else{
                $order = \app\admin\model\OrderExtra::where('order_no', $data['out_trade_no'])->find();
                if(!$order){
                    return json(['code' => 'FAIL', 'message' => '订单不存在'], 500, ['Content-Type' => 'application/json']);
                }
                //判断是否已支付过
                if( $order->paid == 1 ){
                    return false;
                }
                // 修改订单状态
                $order->trade_no = $data['trade_no'];
                $order->paid = 1;
                $order->paidtime = time();
                $order_result = $order->save();

                // 修改结束时间
                $overtime = time() + ($order->duration * 3600);
                $order_result = \app\admin\model\Order::where(['id'=>$order['order_id'],'user_id'=>$order['user_id']])
                    ->update(['status'=>4,'overtime'=>$overtime,'updatetime'=>time()]);
                // 修改管家状态为服务中
                \app\admin\model\Steward::where(['id'=>$order->steward_id])->update(['service_status'=>2]);
            }
            return $order_result;
        }


    }

}
